<?php
declare (strict_types = 1);

namespace app\admin\controller;

use think\Request;
use think\facade\Cache;
class Mymessage extends Base
{
    /**
     * 获取消息信息
     *
     * @return \think\Response
     */
    public function index()
    {
        $roles_data = [];
        foreach ($this->admin->roles as $role) {
            $roles_data[] = $role->id;
        }
        $data = [
            'title'=>'订单',
            'number'=>0,
            'mod'=>'order'
        ];
        if(in_array(6,$roles_data) && !in_array(5,$roles_data)){
            $data['number'] = \app\admin\model\Order::where('status',1)->count();
        }
        return $this->success('ok', $data);

    }
    public function bind()
    {
        $data['client_id']=input('client_id');
        $data['uid']=$this->admin->id;
        app('SendMessage')->bindUid($data);
        return $this->success('绑定'.$this->admin->id.'成功');
    }

    /**
     * 给指定用户发送消息
     * @param int $uid
     * @return void
     * @throws \think\db\exception\DbException
     */
    public static function send(int $uid=6)
    {
        $data['uid']=$uid;
        $number = \app\admin\model\Order::where('status',1)->count();
        $data['message']=[
            'type'=>'client_message',
            'title'=>'订单',
            'number'=>$number,
            'mod'=>'order'
        ];
        app('SendMessage')->sendMsg($data);
    }
    public function publish($event_name,$data)
    {
        if(!empty($data)){
            $redis = Cache::store('redis');
            $redis->publish(['redisSub:'.$event_name], json_encode($data));
        }

    }

}
